package com.masq.aboard.controller.manager;

import com.masq.aboard.common.results.CommonResult;
import com.masq.aboard.common.util.JwtTokenProvider;
import com.masq.aboard.service.RsaKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author masq
 * @since 2024-05-02
 */
@RestController
@RequestMapping("/manager")
public class LoginController {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private RsaKeyService rsaKeyService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    @GetMapping("/getLoginKey")
    public CommonResult<String> getLoginKey(String username) {
        String publicKey = rsaKeyService.getPublicKey(username);
        return CommonResult.success(publicKey);
    }

    // @PostMapping("/login")
    // public CommonResult<String> login(String username, String encryptedPassword) {
    //     // 登录逻辑
    //     // 1. 根据用户名获取用户详细喜喜
    //     UserDetails userDetails = userDetailsService.loadUserByUsername(username);
    //     if (userDetails == null) {
    //         throw new UsernameNotFoundException("找不到该用户");
    //     }
    //     // 2. 根据rsa私钥对前端传过来的密码进行解码
    //     RSA rsa = rsaKeyService.getRsaByAlias(username);
    //     String password = rsa.decryptStr(encryptedPassword, KeyType.PrivateKey);
    //
    //     // 3. 使用密码加密对密码加密并与userDetails中的密码进行对比
    //     if (!passwordEncoder.matches(password, userDetails.getPassword())) {
    //         throw new BadCredentialsException("密码错误");
    //     }
    //
    //     // 4. 生成token
    //     String token = jwtTokenProvider.createToken(userDetails);
    //     return CommonResult.success(token);
    // }

    @PostMapping("/login")
    public CommonResult<String> login(String username, String password) {
        // 登录逻辑
        // 1. 根据用户名获取用户详细喜喜
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (userDetails == null) {
            throw new UsernameNotFoundException("找不到该用户");
        }
        // // 2. 根据rsa私钥对前端传过来的密码进行解码
        // RSA rsa = rsaKeyService.getRsaByAlias(username);
        // String password = rsa.decryptStr(encryptedPassword, KeyType.PrivateKey);

        // 3. 使用密码加密对密码加密并与userDetails中的密码进行对比
        if (!passwordEncoder.matches(password, userDetails.getPassword())) {
            throw new BadCredentialsException("密码错误");
        }

        // 4. 生成token
        String token = jwtTokenProvider.createToken(userDetails);
        return CommonResult.success(token);
    }

    @GetMapping("/logout")
    public CommonResult<?> logout() {
        return CommonResult.success();
    }
}
